﻿using MvvmCross.Wpf.Platform;
using MvvmCross.Wpf.Views;
using Sidvall.FileSystemManager.UI.ViewModels;
using System.Windows.Threading;

namespace Sidvall.FileSystemManager.UI.Wpf
{
    public class ApplicationManager : Sidvall.UI.Wpf.ApplicationManager
    {
        #region Public Members

        #region CreateSetup

        protected override MvxWpfSetup CreateSetup(Dispatcher uiThreadDispatcher, IMvxWpfViewPresenter presenter)
        {
            return new Setup(uiThreadDispatcher, presenter);
        }

        #endregion
        #region OnInitialized

        protected override void OnInitialized()
        {
            var windows = (MainWindow)Sidvall.UI.Wpf.SystemContext.Current.MainWindow;
            var model = new MainWindowViewModel();
            model.Start("");
            windows.ViewModel = model;
        }

        #endregion
        #region RegisterTypes

        protected override void RegisterTypes()
        {
            // Framework
            var authorizationProvider = new Sidvall.Security.PublicAuthorizationProvider();
            Sidvall.SystemContext.Current = new Sidvall.SystemContext()
            {
                //AccountManager = new Sidvall.Security.AccountManager(new Sidvall.FileSystemManager.Security.AccountProvider()),
                AuthorizationManager = new Sidvall.Security.AuthorizationManager(authorizationProvider),
                //AuthenticationManager = new Sidvall.Security.AuthenticationManager(new Sidvall.Security.AuthenticationProvider()),
                AuthenticationStrategyManager = new Sidvall.Security.AuthenticationStrategyManager(new Sidvall.Security.AuthenticationStrategy()),
                //CacheManager = new Sidvall.Caching.CacheManager(new Sidvall.Caching.MemoryCache()),
                //ClaimManager = new Sidvall.Security.ClaimManager(new Sidvall.Security.ClaimsPrincipalManager()),
                //CodeManager = new Sidvall.Reflection.CodeManager(new Sidvall.Reflection.Code()),
                UserExceptionManager = new Sidvall.ExceptionHandling.ExceptionManager(new Sidvall.ExceptionHandling.SystemExceptionProvider()),
                SystemExceptionManager = new Sidvall.ExceptionHandling.ExceptionManager(new Sidvall.ExceptionHandling.SystemExceptionProvider()),
                //HttpClientFactory = new Sidvall.Net.HttpClientFactory(),
                LauncherManager = new Sidvall.SystemEnvironment.LauncherManager(new Sidvall.SystemEnvironment.Windows.Launcher()),
                LogManager = new Sidvall.Logging.LogManager(new Sidvall.Logging.LogCollection()),
                PackageManager = new Sidvall.SystemEnvironment.PackageManager(new Sidvall.SystemEnvironment.Windows.Package()),
                //SmsClientManager = new Sidvall.Net.Sms.SmsClientManager(new Sidvall.Net.Sms.SmsClient("")),
                //SmtpClientManager = new Sidvall.Net.Mail.SmtpClientManager(new Sidvall.Net.Mail.SendGridSmtpClient()),
                //SoapClientFactory = new Sidvall.Net.SoapClientFactory(),
                //SqlManager = new Sidvall.Data.SqlManager(new Sidvall.Data.SqlServerProvider()),
                SystemStorageManager = new Sidvall.IO.StorageManager(new Sidvall.IO.FileSystem()),
                //TransactionScopeFactory = new Sidvall.Data.TransactionScopeFactory(),
                TypeCache = new Sidvall.Reflection.TypeCache(),
                UserManager = new Sidvall.Security.UserManager(authorizationProvider),
                UserResponseStrategyManager = new Sidvall.Security.UserResponseStrategyManager(new Sidvall.Security.UserResponseStrategy()),
                UserStorageManager = new Sidvall.IO.StorageManager(new Sidvall.IO.FileSystem(Sidvall.IO.FolderType.LocalApplicationData)),

                // Client
                PrincipalContextManager = new Sidvall.Security.PrincipalContextManager(new Sidvall.Security.PrincipalContextProvider()),
                //PrincipalManager = new Sidvall.Security.PrincipalManager(new Sidvall.Security.PrincipalProvider()),

                // Host specific
                CultureManager = new Sidvall.Globalization.CultureManager(new Sidvall.UI.Wpf.Globalization.CultureProvider()),
                ResourceManager = new Sidvall.Resources.ResourceManager(new Sidvall.FileSystemManager.UI.Wpf.Resources.ResourceProvider()),
                SettingsManager = new Sidvall.Configuration.SettingsManager(new Sidvall.FileSystemManager.UI.Wpf.Configuration.ApplicationSettings()),
            };
            Sidvall.UI.SystemContext.Current = new Sidvall.UI.SystemContext()
            {
                ApplicationManager = new Sidvall.UI.ApplicationManager(),
                ConfigurationContext = new Sidvall.UI.Configuration.ConfigurationContext(new Sidvall.UI.Wpf.Views.ContentControlRegionManager()),
                DialogManager = new Sidvall.UI.Dialogs.DialogManager(
                    new Sidvall.UI.Wpf.Dialogs.FileDialog(),
                    new Sidvall.UI.Wpf.Dialogs.FolderDialog(),
                    new Sidvall.UI.Wpf.Dialogs.MessageDialogManager(),
                    new Sidvall.UI.Wpf.Dialogs.MessageDialogManager(),
                    new Sidvall.UI.Wpf.Dialogs.SecurityDialogManager(),
                    new Sidvall.UI.Wpf.Dialogs.ProgressDialogManager(),
                    new Sidvall.UI.Wpf.Dialogs.WebDialogManager()),
                NavigationManager = new Sidvall.UI.Navigation.NavigationManager(),
            };
        }

        #endregion

        #endregion
        #region Factory Members

        #region Current

        private static readonly object _Mutex = new object();
        private static volatile ApplicationManager _Current;
        public static ApplicationManager Current
        {
            get
            {
                if (_Current == null)
                {
                    lock (_Mutex)
                    {
                        if (_Current == null)
                        {
                            var item = new ApplicationManager();
                            _Current = item;
                        }
                    }
                }
                return _Current;
            }
            set
            {
                lock (_Mutex)
                {
                    _Current = value;
                }
            }
        }

        #endregion

        #endregion
    }
}
